package com.sx.zongzhi.adapter;


import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sx.zongzhi.R;
import com.sx.zongzhi.activity.SelectCompanyActivity;
import com.sx.zongzhi.bean.CompanyBean;

import java.util.List;

/**
 * Created by XZY on 2017/11/23.
 * 主办单位选择
 */

public class CompanySelectAdapter extends BaseQuickAdapter<CompanyBean.RowsBean,BaseViewHolder> {

    private Context context;
    private CompanyBean companyBean;

    public void setSelect(CompanyBean companyBean){
        if (companyBean!=null){
            this.companyBean=companyBean;
        }
    }

    public CompanySelectAdapter(Context context, @LayoutRes int layoutResId,
                                @Nullable List<CompanyBean.RowsBean> data) {
        super(layoutResId, data);
        this.context=context;
    }

    @Override
    protected void convert(BaseViewHolder helper, final CompanyBean.RowsBean item) {
        CheckBox checkBox=helper.getView(R.id.checkbox);
        checkBox.setText(item.getLabel());
        if (companyBean.getRows()!=null&&companyBean.getRows().size()>0){
            List<CompanyBean.RowsBean> list=companyBean.getRows();
            int count=list.size();
            for (int i=0;i<count;i++){
                if (list.get(i).getValue().equals(item.getValue())){
                    checkBox.setChecked(true);
                }
            }
        }
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    ((SelectCompanyActivity)context).addList(item);
                }else {
                    ((SelectCompanyActivity)context).deleteList(item);
                }
            }
        });
    }
}
